package com.omazan.db;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.omazan.utils.OmazanException;

public class GeneralDBMethods {

	private static GeneralDBMethods instance;
	private static SessionFactory sessionFactory;

	public static GeneralDBMethods getInstance() {

		if (null == instance) {
			GeneralDBMethods.instance = new GeneralDBMethods();
			return GeneralDBMethods.instance;
		} else {

			return GeneralDBMethods.instance;
		}
	}

	private GeneralDBMethods() {

	}

	public SessionFactory getSessionFactoryForDbOperation()
			throws OmazanException {

		if (null == GeneralDBMethods.sessionFactory) {
			Configuration configuration = new Configuration();
			configuration.configure();
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			GeneralDBMethods.sessionFactory = configuration
					.buildSessionFactory(serviceRegistry);

			return GeneralDBMethods.sessionFactory;
		} else {
			return GeneralDBMethods.sessionFactory;
		}
	}

}
